package com.originit.order.server.converter;

import com.originit.order.server.dto.OrderDTO;
import com.originit.order.server.entity.OrderDetail;
import com.originit.order.server.enums.ResultEnum;
import com.originit.order.server.exception.OrderException;
import com.originit.order.server.form.OrderCreateForm;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class OrderConverter {

    public static OrderDTO orderCreateForm2OrderDTO(OrderCreateForm form){
        try{
            List<OrderDetail> detailList = new Gson().fromJson(form.getItems(),new TypeToken<List<OrderDetail>>(){}.getType());
            return OrderDTO.builder()
                    .buyerName(form.getName())
                    .buyerOpenid(form.getOpenid())
                    .buyerPhone(form.getPhone())
                    .detailList(detailList)
                    .buyerAddress(form.getAddress()).build();
        }catch (Exception e){
            log.error("【Json转换】 错误 string = {}",e.getMessage());
            throw new OrderException(ResultEnum.JSON_FORMAT_ERROR);
        }
    }
}
